# -*- coding: utf-8 -*-
# @Time    : 2021/4/9 12:02
# @Author  : Zhangrenjie
# @Email   : zhangrenjie1998@163.com
# @File    : ip_check.py
# @Software: PyCharm

import random


def create_file(filename):
    with open(filename, mode='w', encoding='utf-8') as f:
        for i in range(120000):
            line = f"172.25.254.{random.randint(1, 255)}\n"
            f.write(line)


def counter_top_10(filename):
    result = dict()
    with open(filename, mode='r', encoding='utf-8') as f:
        for ip in f:
            if ip in result:
                result[ip] += 1
            else:
                result[ip] = 1
    # sorted内建函数排序
    result_top10 = sorted(result.items(), key=lambda x: (x[0], x[1]), reverse=True)[:10]
    return result_top10


filename = 'ips.txt'
create_file(filename)
top10 = counter_top_10(filename)
print(top10)
